<?php
require_once(getcwd().'/application/models/SS_model.php');
class User_model extends SS_model
{
	public function __construct()
	{
		parent::__construct();
		$this->load->database();
		$this->load->library('session');
		$this->load->library('encryption');
	}


	public function getLoginVerifyState($data)
	{	
		if( array_key_exists("user",preg_grep("/^[0-9a-zA-z\.]+@motionglobal.com$/",$data)) && array_key_exists("passwords", preg_grep("/^[$#@0-9a-zA-z]{6,20}$/", $data)))
		{
			$sql = 'SELECT auto_id as user_id FROM `users` WHERE user_id = ? AND passwords = ? ;';
			$result = $this->db->query($sql,array($data['user'],sha1($data['passwords'])))->row_array();
			if(isset($result['user_id']))
			{
				$_SESSION['user_state'] = true;
				$_SESSION['user_id'] = $result['user_id'];
				return array("code"=>"200");
			}
			return array("code"=>"101","text"=>"Wrong email address Or password");//101 用户名密码错误
		}
		return array("code"=>"102","text"=>"Please enter email and password in correct format");//102 不符合规范
	}

	public function getRegisterState($data)
	{
		if ( array_key_exists("user",preg_grep("/^[0-9a-zA-z\.]+@motionglobal.com$/",$data)))
		{
			if(strlen($data['pass']) >=6 && ($data['pass'] == $data['con_pass'] ))
			{
				$_SESSION['con_user'] = $data['user'];
				$_SESSION['con_pass'] = sha1($data['pass']);
				$url = "http://alexfu.cc/user/register/confirm";
				$content = "Hello,We'd be glad to receive your registration application. \nPlease click this link to confirm your Registration \n".$url."\nIf this mail is not for you ,please ignore it ,thanks \n\n\n Best Wishes \n MotionGlobal";
				mail($data['user'],"Please Confirm Your Registration",$content);
				return array("code"=>"200");
			}
			
		}
		return array("code"=>"101","text"=>"Wrong email address");
		
	}

	public function registerConfirmState()
	{
		if(isset($_SESSION['con_user']) && isset($_SESSION['con_pass']))
		{
			$sql = 'INSERT INTO `users`(`auto_id`, `user_id`, `passwords`, `mail`, `phone`,`is_hr`,`is_leader`) VALUES (null,?,?,?,?,?,?);';
			$this->db->query($sql,array($_SESSION['con_user'],$_SESSION['con_pass'],$_SESSION['con_user'],"0","no","no"));
			unset($_SESSION['con_user']);
			unset($_SESSION['con_pass']);
			return array("code"=>"200");
		}
		return array("code"=>"101","text"=>"Overtime");
		
	}

	public function getProfileState($data)
	{
		return $this->_redisKeyInterFace("_getProfileState","_getProfileState",$data);
	}

	public function _getProfileState($data)
	{
		$sql = 'SELECT * FROM `profile` WHERE user_id = ? ;';
		$result = $this->db->query($sql,array($data['user_id']))->row_array();
		if(isset($result['user_id']))
		{
			return $result;
		}
		$sql = "INSERT INTO `profile` (user_id,user_name,title,department) VALUES (?,?,?,?) ;";
		$this->db->query($sql,array($data['user_id'],"Anonymous","Not Defined","Not Defined"));
		return array("user_id"=>$data['user_id'],"user_name"=>"Anonymous","title"=>"Not Defined","department"=>"Not Defined");
	}

	public function editProfile($data)
	{
		$sql = "UPDATE `profile` SET user_name = ? ,title = ? ,department = ? WHERE user_id = ? ;";
		$this->db->query($sql,array($data['user_name'],$data['title'],$data['department'],$_SESSION['user_id']));
		return array("code"=>"200");
	}

	public function isLeader()
	{
		$sql  = "SELECT is_leader FROM users WHERE auto_id = ? ;";
		return $this->db->query($sql,array($_SESSION['user_id']))->row_array()['is_leader'];
	}

	public function getLeaveAndSickDaysState()
	{
		$sql = "SELECT leave_balance,sick_leave_balance FROM leave_days WHERE user_id = ? ORDER BY cal_year DESC LIMIT 2 ;";
		$data = $this->db->query($sql,array($_SESSION['user_id']))->result_array();
		$res = array();
		$res['leave_days'] = $data[0]['leave_balance']+$data[1]['leave_balance'];
		$res['sick_days'] = $data[0]['sick_leave_balance'];
		return $res;
	}
	//public function 
}